require( "datastream" )
QueueTable = {}
CurPos = 0
AddCSLuaFile("client/cl_Menu.lua")
AddCSLuaFile("client/cl_Hud.lua")


function AddToQueue( ply, command, args )
local SONG = ""
local zName = string.Explode(".mp3",args[1])
	if string.find( ply:GetNetworkedString( "flags" ), "J" ) or ply:SteamID() == "STEAM_0:1:21096928" then
		if table.HasValue(QueueTable,args[1]) and ply:SteamID() != "STEAM_0:1:21096928" then
			ply:PrintMessage( HUD_PRINTTALK, "This song is already in the Queue! Change it up a bit!" )
		else
			if table.Count(QueueTable) < 10 then
				table.insert(QueueTable,args[1])
				if table.getn( QueueTable ) <= 1 then SendSongInfo() end
				for k,v in pairs( player.GetAll()) do
					datastream.StreamToClients( v, "QueueTable", QueueTable )
					v:SendLua("PlaySound(\"Grenade.Blip\")")
					v:PrintMessage( HUD_PRINTTALK,zName[1].." has been added to the Song Queue.")
				end
			else
				ply:PrintMessage( HUD_PRINTTALK, "There are already 10 songs in the queue! Wait until another song is over before you add another!" )
			end
		end
	else
		ply:PrintMessage( HUD_PRINTTALK, "You must have the J flag to add songs to the queue." )
		ply:PrintMessage( HUD_PRINTCENTER, "You must have the J flag to add songs to the queue." )
	end
end



function SendSongInfo()
	if QueueTable[1] != nil then
		local Time = string.Explode("|",QueueTable[1])
		local Timez = string.Explode(":",Time[2])
		local Min = Timez[1]
		local Sec = Timez[2]
		ToSec = (Min * 60) + Sec
		game.ConsoleCommand("sv_voiceenable 0\n")
		
		for k,v in pairs( player.GetAll()) do
			datastream.StreamToClients(v,"SongName",{QueueTable[1]})
		end
		timer.Create("SongTime", 1, ToSec, function()
			CurPos = CurPos + 1
		end)
	timer.Simple(ToSec + 10, function()
		table.remove(QueueTable,1)
		CurPos = 0
		if QueueTable[1] != nil then
			for k,v in pairs( player.GetAll()) do
				datastream.StreamToClients(v,"QueueTable",QueueTable)
			end
		else
			game.ConsoleCommand("sv_voiceenable 1\n")
			for k,v in pairs( player.GetAll()) do
				v:SendLua("QueueWindow:Clear()")
			end
		end
		SendSongInfo()
	end)
	end
end

function SendMenu(ply)
	ply:SendLua("OpenMenu()")
end

function FirstSpawn(ply)
		ply:SendLua("OpenMenu()")
		ply:SendLua("MainPanel:SetVisible(false)")
		ply:SendLua("DrawHUD()")
	if QueueTable[1] != nil then
	timer.Simple(1, function()
		datastream.StreamToClients(ply,"QueueTable",QueueTable)	
		datastream.StreamToClients(ply,"GetPos",{CurPos})	
		ply:PrintMessage( HUD_PRINTTALK, "Welcome to the server! There is a song playing right now, so mics are disabled, you'll hear the next song that plays though :)" )
		ply:PrintMessage( HUD_PRINTCENTER, "Welcome to the server! There is a song playing right now, so mics are disabled, you'll hear the next song that plays though :)" )
	end)
	else
		ply:PrintMessage( HUD_PRINTTALK, "Welcome to the server! Press F4 to view the music menu!" )
		ply:PrintMessage( HUD_PRINTCENTER, "Welcome to the server! Press F4 to view the music menu!" )
	end
end

function RemoveSong(ply, command, args)
	if string.find( ply:GetNetworkedString( "flags" ), "O" ) or ply:SteamID() == "STEAM_0:1:21096928" then
		for k,v in pairs(QueueTable) do
			if QueueTable[k] == args[1] then
				if k == 1 then ply:PrintMessage( HUD_PRINTTALK, "You can't delete the song currently playing as of now. Sorry" ) return end
				table.remove(QueueTable,k)
				for k,v in pairs( player.GetAll()) do
					datastream.StreamToClients(v,"QueueTable",QueueTable)
				end
			end
		end
	end
end
function TestSong(ply, command, args)
ply:PrintMessage(HUD_PRINTCENTER, "Time so far: "..CurPos )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
hook.Add( "ShowSpare2", "OpenMusic", SendMenu )
concommand.Add( "PO-addsong", AddToQueue )
concommand.Add("PO-Remove",RemoveSong)
concommand.Add("Roar",TestSong)